Ext.extend(avatar.module, {
	dateRange : 1,
	init : function() {
        var me = this;
        var store = new Ext.data.JsonStore({
			fields : ["id", "trackingNumber", "maker.name", "whenOccurred", "usageDelayDays"]
		}),
		w_items_ds = new Ext.data.JsonStore({
		    fields : ["product.name", "quantityOfColumn", "quantityOfBlock"]
		});

	    ProductManager.findRecentWarehousings(1, function(data){
		    store.loadData(data);
		});
		

		
	/**	store.on('update', function(self, record){
			var role_filter = Ext.getCmp('party-role-filter');
			var updatedRec =  {id : record.get("id"), name : record.get("name"), 
			                   phoneNumber: record.get('phoneNumber'), 
			                   role : me.getRole(role_filter.selectedIndex), active: record.get('active')}; 	
			PartyManager.saveParty(updatedRec, function(data){			
			    role_filter.fireEvent('select', null, null, role_filter.selectedIndex);
			});

		});
		
		var Party = Ext.data.Record.create([
			{ name: 'name', type: 'string' },
			{ name: 'phoneNumber', type: 'string' }		
		]);
        var WarehousingItem = Ext.data.Record.create([
			{ name: 'productId', type: 'int' },
			{ name: 'quantityOfColumn', type: 'int' },
			{ name: 'quantityOfBlock', type: 'int' }
		]);*/
        var tbar = 
				 ['报表类型: ', ' ',
                   new Ext.ux.SelectBox({
                     listClass:'x-combo-list-small',
                     width:93,
                     value:'当天',
                     id:'warehousing-date-range-filter',
                     store: new Ext.data.SimpleStore({
                        fields: ['d'],
                        expandData: true,
                        data : [ '运输班组月对帐单','产品库存变更','近30天' ]
                     }),
                     displayField: 'd',
					 listeners: {
                       select: function(self, rec,index){
                       	   me.dateRange = 1;
                           if (index == 1) me.dateRange = 7;
                           if (index == 2) me.dateRange = 30;

                           ProductManager.findRecentWarehousings(me.dateRange, function(data){
								store.loadData(data);
						   });
					   }

					 }
                   }), ' ',
                   new Ext.Toolbar.Button({ id: 'warehousing-add-btn', text:'新建', handler : function(){ 				   
				        var maker_ds = new Ext.data.JsonStore({
							fields : ["id", "name", "phoneNumber"]
						}),
						product_ds = new Ext.data.JsonStore({
							fields : ["id", "name", "salePrice", "quantityOfColumn", "quantityOfBlock"]
						}),
						product_sm = new Ext.grid.CheckboxSelectionModel(),
						product_cm = new Ext.grid.ColumnModel([
							product_sm,
							{  dataIndex: 'id', hidden: true },
							{
							   header: '名称',
							   dataIndex: 'name',
							   sortable: true,
							   width: 130
							},{
					            xtype: 'numbercolumn',
					            header: '条数',
					            dataIndex: 'quantityOfColumn',
					            width: 55,
					            format: '0',
					            editor: {
					                xtype: 'numberfield',
					                minValue: 0
					            }
					        },{
					            xtype: 'numbercolumn',
					            header: '块数',
					            dataIndex: 'quantityOfBlock',
					            width: 55,
					            format: '0',
					            editor: {
					                xtype: 'numberfield',
					                minValue: 0
					            }
					        }
							]
						);
				        PartyManager.getAllActivePartyByRole('maker', function(data){
						    maker_ds.loadData(data);
					    });
					    ProductManager.getAllProduct( function(data){
							product_ds.loadData(data);
						});
		               var form = new Ext.FormPanel({
			                labelWidth: 80, 
		            		frame: true,
							defaults: {anchor: '98%'},
							defaultType: 'textfield', 				
			        		items : [
			        		  {xtype:'combo', fieldLabel:'生产班组', name: 'maker',
			        		     store : maker_ds,
			        		     id: 'warehousing-maker-combo',
						         forceSelection : true,
						         displayField: 'name',
						         valueField:'id',
								 editable:false,
								 typeAhead: true,
								 mode: 'local',
								 triggerAction: 'all',
								 selectOnFocus:true,
								 allowBlank: false		        		  
			        		  } ,
			        		  {xtype:'numberfield', id: 'warehousing-cache-days', fieldLabel:'缓存天数', name: 'usageDelayDays',emptyText : '0', allowNegative : false}
			        		  ,
			        		  {
			        		  	xtype:'editorgrid',
			        		  	id: 'new-warehousing-product-grid',
			        		  	store: product_ds,
								cm: product_cm,
								sm: product_sm,
								autoWidth: true,
								height:250,
								frame:true,
								clicksToEdit:1			        		  	
			        		  }
			        		],
			        		buttons: [{
			                    text:'提交',
			                    handler: function(){
			                    	
			                    	var pass = form.getForm().isValid();
			                    	if (pass){
			                    		var grid = Ext.getCmp('new-warehousing-product-grid');
			                    		grid.stopEditing();
			                    		var selectedRows = grid.getSelectionModel().getSelections();
			                    		if (selectedRows.length == 0){
			                    		    alert("至少需要选择一个产品。")
			                    		    return;
			                    		}
			                    		var items = new Array();
			                    		var confirmItems = "请再次确认入库单明细:<br/>";
			                    		for (i=0,cnt = selectedRows.length ;i<cnt;i+=1){
			                    		    var rec = selectedRows[i];		
			                    		    items[i] = new WarehousingItemDto();
			                    		    items[i].productId = rec.get("id"); 
			                    		    items[i].quantityOfColumn = rec.get("quantityOfColumn");
			                    		    items[i].quantityOfBlock = rec.get("quantityOfBlock");
			                    		    
			                    		    confirmItems += "<li>" + rec.get("name") + "(条数:" + items[i].quantityOfColumn
			                    		       + ",块数:" + items[i].quantityOfBlock + ")</li>"
			                    		}
                                        Ext.MessageBox.alert('Hello', confirmItems);
			                    		
			                    		
			                    		var makerId = Ext.getCmp('warehousing-maker-combo').getValue(),
			                    		    cacheDays = Ext.getCmp('warehousing-cache-days').getValue();

			                    	/**	ProductManager.saveWarehousingBill(items, makerId, cacheDays, function(data){
			                    		    ProductManager.findRecentWarehousings(me.dateRange, function(data){
												store.loadData(data);
												win.close();
										    });
			                    		
			                    		});*/
			                    	}
			                    }
			                },{
			                    text: '关闭',
			                    handler: function(){
			                        win.close();
			                    }
			                }]
		               
		               });     	
		               var win =  new Ext.Window({//window config
						    constrain : true,
							layout : 'fit',
							title: '新建入库单',
							width : 400,
							closeAction : 'close',
							height : 390,
							resizable : false,
							shadow : true,
							modal : true,
							closable : true,
							border : false,
							animCollapse : true,
							items : [form]
							
						});
						
						win.show();

				   } })
			    ];

        var sm = new Ext.grid.RowSelectionModel({singleSelect:true});
        sm.on('selectionchange', function(sm){	
		    var record = sm.getSelected();
            if (record)
		    ProductManager.getWarehousingById(record.get('id'), function(data){
		        w_items_ds.loadData(data.items);
		    });
		    else
		        w_items_ds.removeAll();
		    
        });
        
		this.tabpanel.add(
			{
				xtype:'grid',
				title:'月报表',
				closable:true,
				id: 'month-report',
				region: 'center',
				margins: '2 0 2 2',
				store : store, 
                tbar: tbar,
                sm: sm,
				columns:[
					new Ext.grid.RowNumberer(),
					{  dataIndex: 'id', hidden: true },
					{
					   header: '单号',
					   dataIndex: 'trackingNumber',
					   sortable: true,
					   width: 100
					},
					{
					   header: '生产班组',
					   dataIndex: 'maker.name',
					   sortable: true,
					   width: 230
					},											
					{   xtype: 'datecolumn',
					    format:'Y-m-d H:i',
						header: '入库日期',
						dataIndex: 'whenOccurred',
						width: 100
					},
					{   xtype: 'numbercolumn',
					    format: '0',
						header: '缓存天数',
						dataIndex: 'usageDelayDays',
						width: 75
					}
					]				
     
          
          
		  }).show();        

	}
});

